// pages/managerTool/index.js
import { restTime } from '../../utils/restTime.js'
let clickFlag = false
let homeInterval
let userInterval
let recommendInterval
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    loading1: false,
    loadingHeader: false,
    showDeleteModal: false,
    homeText: '',
    homeRestTime: '',
    homeConstantTime: '',
    homeState: false,
    userText: '',
    userRestTime: '',
    userConstantTime: '',
    userState: false,
    recommendRestTime: '',
    recommendText1: '',
    recommendImg1: '',
    img1Src: '',
    recommendImg1State: false,
    recommendText2: '',
    recommendImg2: '',
    img2Src: '',
    recommendImg2State: false,
    recommendConstantTime: '',
    recommendState: false
  },
  hideModal() {
    clickFlag = false
    this.setData({
      showDeleteModal: false
    })
  },
  ChooseBgImage1() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          recommendImg1: res.tempFilePaths[0],
          recommendImg1State: true
        })
      }
    });
  },

  ChooseBgImage2() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          recommendImg2: res.tempFilePaths[0],
          recommendImg2State: true
        })
      }
    });
  },
  homeSwitch(e){
    this.setData({
      homeState: e.detail.value
    })
  },
  getHomeText(e) {
    this.setData({
      homeText: e.detail.value
    })
  },
  getHomeConstantTime(e) {
    this.setData({
      homeConstantTime: e.detail.value
    })
  },
  userSwitch(e) {
    this.setData({
      userState: e.detail.value
    })
  },
  getUserText(e) {
    this.setData({
      userText: e.detail.value
    })
  },
  getUserConstantTime(e) {
    this.setData({
      userConstantTime: e.detail.value
    })
  },
  recommendSwitch(e) {
    this.setData({
      recommendState: e.detail.value
    })
  },
  getRecommendConstantTime(e) {
    this.setData({
      recommendConstantTime: e.detail.value
    })
  },
  getRecommendText1(e) {
    this.setData({
      recommendText1: e.detail.value
    })
  },
  getRecommendText2(e) {
    this.setData({
      recommendText2: e.detail.value
    })
  },
  beforeModify(){
    if (clickFlag) return //防抖处理
    else clickFlag = true
    this.setData({
      showDeleteModal: true
    })
  },
  comfireModified(){
    let request1 = false
    let request2 = false
    this.setData({
      loading: true
    })
    const request3 = () => {
      if (request1 && request2) {
        wx.cloud.callFunction({
          name: 'submitManagerTool',
          data: {
            mode: 'content',
            homeText: this.data.homeText,
            userText: this.data.userText,
            recommendText1: this.data.recommendText1,
            recommendImg1: this.data.img1Src == '' ? this.data.recommendImg1 : this.data.img1Src,
            recommendText2: this.data.recommendText2,
            recommendImg2: this.data.img2Src == '' ? this.data.recommendImg2 : this.data.img2Src
          }
        }).then(res => {
          clickFlag = false
          this.setData({
            loading: false
          })
          wx.showToast({
            title: '修改成功！',
            icon: 'success',
            duration: 2000
          })
        })
      }
    }
    if (this.data.recommendImg1State){
      wx.cloud.deleteFile({
        fileList: [this.data.recommendImgCache1]
      }).then(del => {
        wx.cloud.uploadFile({
          cloudPath: 'recommendImg/img1-' + new Date().getTime(),
          filePath: this.data.recommendImg1, // 文件路径
        }).then(res => {
          request1 = true
          this.data.img1Src = res.fileID
          request3()
        })
      })
    }else{
      request1 = true
      request3()
    }
    if (this.data.recommendImg2State){
      wx.cloud.deleteFile({
        fileList: [this.data.recommendImgCache2]
      }).then(del => {
        wx.cloud.uploadFile({
          cloudPath: 'recommendImg/img2-' + new Date().getTime(),
          filePath: this.data.recommendImg2, // 文件路径
        }).then(res => {
          request2 = true
          this.data.img2Src = res.fileID
          request3()
        })
      })
    }else{
      request2 = true
      request3()
    }
    
  },
  comfireModified1(){
    this.setData({
      loading1: true,
      showDeleteModal: false
    })
    wx.cloud.callFunction({
      name: 'submitManagerTool',
      data: {
        mode: 'status',
        homeConstantTime: this.data.homeConstantTime,
        homeState: this.data.homeState,
        userConstantTime: this.data.userConstantTime,
        userState: this.data.userState,
        recommendConstantTime: this.data.recommendConstantTime,
        recommendState: this.data.recommendState
      }
    }).then(res => {
      clickFlag = false
      this.setData({
        loading1: false
      })
      this.getData()
      wx.showToast({
        title: '修改成功！',
        icon: 'success',
        duration: 2000
      })
    })
  },
  getData(){
    this.setData({
      loadingHeader: true
    })
    wx.cloud.callFunction({
      name: 'getManagerData',
      data: {
        caller: 'manager'
      }
    }).then(res => {
      this.setData({
        loadingHeader: false
      })
      let managerData = res.result

      let homeFutureTime = managerData.homeTextCreateDate + Number(managerData.homeConstantTime) * 24 * 3600 * 1000
      homeInterval = setInterval(() => {
        let homeRestTime = restTime(homeFutureTime)
        if (homeRestTime){
          this.setData({
            homeRestTime
          })
        }else{
          clearInterval(homeInterval)
          this.setData({
            homeRestTime: '已结束'
          })
        }
      }, 1000)

      let userFutureTime = managerData.userTextCreateDate + Number(managerData.userConstantTime) * 24 * 3600 * 1000
      userInterval = setInterval(() => {
        let userRestTime = restTime(userFutureTime)
        if (userRestTime) {
          this.setData({
            userRestTime
          })
        } else {
          clearInterval(userInterval)
          this.setData({
            userRestTime: '已结束'
          })
        }
      }, 1000)

      let recommendFutureTime = managerData.recommendCreateDate + Number(managerData.recommendConstantTime) * 24 * 3600 * 1000
      recommendInterval = setInterval(() => {
        let recommendRestTime = restTime(recommendFutureTime)
        if (recommendRestTime) {
          this.setData({
            recommendRestTime
          })
        } else {
          clearInterval(recommendInterval)
          this.setData({
            recommendRestTime: '已结束'
          })
        }
      }, 1000)

      this.setData({
        homeText: managerData.homeText,
        homeConstantTime: managerData.homeConstantTime,
        homeState: managerData.homeState,
        userText: managerData.userText,
        userConstantTime: managerData.userConstantTime,
        userState: managerData.userState,
        recommendText1: managerData.recommendText1,
        recommendImg1: managerData.recommendImg1,
        recommendImgCache1: managerData.recommendImg1,
        recommendText2: managerData.recommendText2,
        recommendImg2: managerData.recommendImg2,
        recommendImgCache2: managerData.recommendImg2,
        recommendConstantTime: managerData.recommendConstantTime,
        recommendState: managerData.recommendState
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(homeInterval)
    clearInterval(userInterval)
    clearInterval(recommendInterval)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})